﻿/*****************************************************************************  

 Version: 0.1
 Website: http://www.visuagile.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VisualAgile.Model;
using System.Collections;

namespace VisualAgile.DTO.Mappers
{
    public class FeatureDTOMapper:BaseDTOMapper
    {
        public static FeatureDTO MapToDTO(Feature model)
        {            
            FeatureDTO dto = new FeatureDTO();
            dto.ID = model.FeatureID;
            dto.ReleaseID = model.ReleaseID;
            dto.Name = model.Name;
            dto.Observations = model.Observations;
            dto.Card = model.Card;
            dto.Color = model.Color;
            dto.Target = model.Target;
            dto.CreateTime = model.CreateTime;
            dto.Creator = model.Creator;

            IList stories = ManagerFactory.StoryManager.GetByFeature(model.FeatureID.ToString());            
            double Size = 0.0;
            double CompletedSize = 0.0;            
            DefaultStatus? FeatureStatus = null;            

            foreach (Story story in stories)
            {
                StoryDTO storyDto = StoryDTOMapper.MapToDTO(story);

                if (FeatureStatus == null)
                    FeatureStatus = storyDto.Status;

                if (storyDto.Status == DefaultStatus.NotDone && FeatureStatus == DefaultStatus.NotDone)
                    FeatureStatus = DefaultStatus.NotDone;
                else if (storyDto.Status == DefaultStatus.Done && FeatureStatus == DefaultStatus.Done)
                {
                    FeatureStatus = DefaultStatus.Done;
                    CompletedSize += storyDto.Size;
                }
                else if (storyDto.Status == DefaultStatus.Done)
                    CompletedSize += storyDto.Size;
                else
                    FeatureStatus = DefaultStatus.InProgress;

                //if (CurrentStatus != null && CurrentStatus != DefaultStatus.InProgress)
                //    CurrentStatus = storyDto.Status;
                //else if ((DefaultStatus)Enum.Parse(typeof(DefaultStatus), Status) != DefaultStatus.InProgress)
                //    isCompleted = storyDto.Status;
                //else
                //    CurrentStatus = null;

                //if (CurrentStatus == DefaultStatus.Done && (CurrentStatus == DefaultStatus.Done || CurrentStatus == DefaultStatus.NotDone))
                //{
                //    FeatureStatus = storyDto.Status;
                //    //Status = storyDto.Status;
                //    CompletedSize += storyDto.Size;
                //}
                //else if (CurrentStatus == DefaultStatus.Done || isCompleted != null)
                //{
                //    CompletedSize += storyDto.Size;
                //    isCompleted = null;
                //}
                //else if (CurrentStatus == null || CurrentStatus == DefaultStatus.InProgress)
                //{
                //    FeatureStatus = DefaultStatus.InProgress;
                //    //Status = "InProgress";
                //}

                Size += storyDto.Size;
            }

            //set the current status 
            dto.Status = (FeatureStatus==null) ? DefaultStatus.NotDone : (DefaultStatus)FeatureStatus;

            //set the story size
            dto.Full_Story_Size = Size;
            dto.Finished_Story_Size = CompletedSize;
            dto.Finished_Percentage = GetPercentage(CompletedSize, Size, 1); ;
          
            return dto;
        }

        /// <summary>
        /// Generates a percentage, formatted with "places" decimal places.
        /// </summary>
        /// <param name="value">Value for which a percentage is needed</param>
        /// <param name="total">Total of all values from which to generate a percentage</param>
        /// <param name="places">How many decimal places to return in the percentage string</param>
        /// <returns>string with the percentage value</returns>
        public static string GetPercentage(double value, double total, int places)
        {
            Decimal percent = 0;
            string retval = string.Empty;
            String strplaces = new String('0', places);
            //if (value == null) value = 0;
            //if (total == null) total = 0;
            if (value == 0 || total == 0)
            {
                percent = 0;
            }
            else
            {
                percent = Decimal.Divide(Convert.ToDecimal(value), Convert.ToDecimal(total)) * 100;
                //percent = (Decimal)(((value * total) / 100) * total);
                //if (places > 0) { strplaces = "." + strplaces; }
                strplaces = "." + strplaces;
            }
            retval = percent.ToString("#" + strplaces);

            return retval + "%";
        }

        public static Feature MapFromDTO(FeatureDTO dto)
        {
            Feature Feature = new Feature();
            Feature.FeatureID = dto.ID;
            Feature.ReleaseID = dto.ReleaseID;            
            Feature.Name = dto.Name;
            Feature.Color = dto.Color;
            Feature.Target = dto.Target.ToLocalTime();
            Feature.Card = dto.Card;
            Feature.Observations = dto.Observations;
            Feature.CreateTime = dto.CreateTime;
            Feature.Creator = dto.Creator;
            return Feature;
        }
    }
}
 